package com.ruoyi.web.controller.base;

import com.ruoyi.base.domain.BaseGood;
import com.ruoyi.base.service.IBaseGoodService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.SystemType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.domain.DataInventory;
import com.ruoyi.data.domain.DataInventoryRecord;
import com.ruoyi.data.service.IDataInventoryRecordService;
import com.ruoyi.data.service.IDataInventoryService;
import com.ruoyi.data.service.IDataSaleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品信息Controller
 *
 * @author zzp
 * @date 2022-09-26
 */
@RestController
@RequestMapping("/base/good")
public class BaseGoodController extends BaseController {
    @Autowired
    private IBaseGoodService baseGoodService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IDataSaleService saleService;

    @Autowired
    private IDataInventoryService inventoryService;

    @Autowired
    private IDataInventoryRecordService inventoryRecordService;

    /**
     * 查询商品信息列表
     */
//    @PreAuthorize("@ss.hasPermi('base:good:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseGood baseGood) {
        startPage();
        List<BaseGood> list = baseGoodService.selectBaseGoodList(baseGood);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('base:good:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseGood baseGood) {
        List<BaseGood> list = baseGoodService.selectBaseGoodList(baseGood);
        ExcelUtil<BaseGood> util = new ExcelUtil<BaseGood>(BaseGood.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:good:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(baseGoodService.selectBaseGoodById(id));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('base:good:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseGood baseGood) {
        BaseGood code = baseGoodService.selectBaseGoodByGoodCode(baseGood.getGoodCode());
        if (StringUtils.isNotNull(code)) {
            return AjaxResult.error("商品代码已存在");
        }
        baseGoodService.insertBaseGood(baseGood);

        SysUser user = userService.selectUserById(100L);
        DataInventory inventory = new DataInventory();
        inventory.setDeptId(user.getDeptId());
        inventory.setUserId(user.getUserId());
        inventory.setNickName(user.getNickName());
        inventory.setGoodId(baseGood.getId());
        inventory.setGoodCode(baseGood.getGoodCode());
        inventory.setGoodName(baseGood.getGoodName());
        inventory.setAmount(1000000000);
        inventoryService.insertDataInventory(inventory);

        DataInventoryRecord inventoryRecord = new DataInventoryRecord();
        inventoryRecord.setDeptId(user.getDeptId());
        inventoryRecord.setUserId(user.getUserId());
        inventoryRecord.setNickName(user.getNickName());
        inventoryRecord.setBillNumber("添加商品生成");
        inventoryRecord.setGoodId(baseGood.getId());
        inventoryRecord.setGoodCode(baseGood.getGoodCode());
        inventoryRecord.setGoodName(baseGood.getGoodName());
        inventoryRecord.setAmount(1000000000);
        inventoryRecord.setInAmount(1000000000);
        inventoryRecord.setOutAmount(0);
        inventoryRecord.setStockType(SystemType.IN.getCode());
        inventoryRecord.setStockDesc("添加商品生成");
        inventoryRecordService.insertDataInventoryRecord(inventoryRecord);

        return AjaxResult.success();
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('base:good:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseGood baseGood) {
        BaseGood old = baseGoodService.selectBaseGoodById(baseGood.getId());
        if (!old.getGoodName().equals(baseGood.getGoodName())) {
            //同步订单信息和库存信息
            saleService.updateDataSaleSync(baseGood);
        }
        return toAjax(baseGoodService.updateBaseGood(baseGood));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('base:good:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(baseGoodService.deleteBaseGoodByIds(ids));
    }
}
